home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_01_01 / 1n01026a < prev    next >
Text File  |  1990-05-16  |  9KB  |  296 lines

  1. * Tim Parker
  2. * 6075 Meadowhill Crescent, Gloucester, Ontario, K1C 3N3
  3. * (613) 830-7727 [h], (613) 238-2100 [w], (613) 830-2110 [fax]
  4. * Borland's Paradox Engine for C
  5. * March 20th, 1990 - Listing 1
  6.  
  7. LISTING 1
  8.  
  9. /* IMPORT.C: Converts ASCII to Paradox Table Format */
  10. /* Copyright (c) 1990 by Borland International, Inc. */
  11. /* Used by permission. */
  12. /* Comments modified or removed by Tech Specialist */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include "pxengine.h"
  18.  
  19. /*  IMPORT.C
  20.  *  ASCII Files are expected to be in the following format
  21.  *    Customer Number:     8 characters
  22.  *    Part Number:         8 characters
  23.  *    Quantity:            Integer
  24.  *    Date:                10 characters, format MM/DD/YYYY
  25.  *  Each field within the ASCII record should be separated by
  26.  *  a space. Each record within the ASCII file should be
  27.  *  separated by a newline.
  28.  */
  29.  
  30. /* If using a network, the following is uncommented
  31. #define NETWORK */
  32. #ifdef NETWORK
  33. #define NETUSERNAME    "username"    /* network username */
  34. #define NETPATH        ""            /* .net directory */
  35. #define NETTYPE        NOTONNET      /* network type */
  36. #endif
  37.  
  38. /* Field numbers of Paradox Table Fields */
  39. #define FIELDPARTNBR       1            /* Part Number */
  40. #define FIELDCUSTNBR       2            /* Customer Number */
  41. #define FIELDQUANTITY      3            /* Quantity */
  42. #define FIELDDATE          4            /* Date */
  43.  
  44. /* Sizes of ASCII table fields */
  45. #define ASCIISIZECUSTNBR   9            /* Customer Number Length */
  46. #define ASCIISIZEPARTNBR   9            /* Part Number Length */
  47. #define ASCIISIZEDATE      11           /* Date Length */
  48.  
  49. /* Function Returns */
  50. #define SUCCESS        0            /* function succeeded */
  51. #define FAIL          -1            /* function failed */
  52.  
  53. /* Structure for an ASCII fixed-length record */
  54. typedef struct 
  55. {
  56.     char CustNbr[ASCIISIZECUSTNBR];        /* customer number */
  57.     char PartNbr[ASCIISIZEPARTNBR];        /* part number */
  58.     int quantity;                  /* quantity */        
  59.     char date[ASCIISIZEDATE];              /* date */
  60. } AsciiRecord;
  61.  
  62. /* Field names of Paradox Table */
  63. char *FieldNames[] = 
  64. {
  65.     "Part Number", 
  66.     "Cust Number",
  67.     "Quantity",
  68.     "Date"
  69. };
  70.  
  71. /* Field Types of Paradox table */
  72. char *FieldTypes[] = 
  73. {
  74.     "A8",
  75.     "A8",
  76.     "N",
  77.     "D"
  78. };
  79.  
  80. /* FIelds to be used in PRIMARY field of the Paradox table */
  81. FIELDHANDLE keys[] = 
  82. {
  83.     FIELDPARTNBR,
  84.     FIELDCUSTNBR
  85. };
  86.  
  87. #define NBRFIELDS    (sizeof(FieldNames) / sizeof(char *))
  88. #define NBRKEYS    (sizeof(keys) / sizeof(FIELDHANDLE))
  89.  
  90. /* Function Prototypes */
  91. int main(int, char**);
  92. int OpenFiles(char**,TABLEHANDLE *, FILE **);
  93. int OpenAsciiFile(char *,FILE **);
  94. int CreateParadoxFile(char *);
  95. int translate(TABLEHANDLE, FILE *);
  96. int TranslateBuffer(ASCIIRecord *, RECORDHANDLE);
  97. void CloseFiles(TABLEHANDLE, FILE *);
  98. int Error(int);
  99.  
  100. int main(int argc, char **argv)
  101. {
  102.     FILE * fpAscii;                   /* file pointer to ASCII file */
  103.     TABLEHANDLE tblHandle;            /* table handle to paradox table */
  104.     if (argc != 3)                       /* checks for three arguments */
  105.     {
  106.         printf("usage: IMPORT <ascii_file> <paradox_file>\n");
  107.         return(FAIL);
  108.     }
  109.  
  110.     /* Initialize the engine - change comments if network */
  111. #ifndef NETWORK
  112.     if (Error(PXInit())) 
  113. #else    
  114.     if (Error(PXNetInit(NETPATH, NETTYPE, NETUSERNAME))) 
  115. #endif
  116.         exit(1);
  117.  
  118.     if (CreateParadoxFile(argv[2]) == FAIL)
  119.         exit(1);
  120.  
  121.     /* Open ASCII file and paradox file */
  122.     if (OpenFiles(argv,&tblHandle,&fpAscii) == SUCCESS)    
  123.     {
  124.         translate(tblHandle,fpAscii);
  125.         CloseFiles(tblHandle,fpAscii);
  126.     }
  127.  
  128.     return(Error(PXExit()));
  129. }
  130.  
  131.  
  132. /*    Function:
  133.  *        OpenFiles
  134.  *    Arguments:
  135.  *        argv            Pointer to command line arguments containing 
  136.  *                        file names
  137.  *        tblHandlePtr    Pointer to a Paradox Table Handle
  138.  *        fpAscii         Pointer to a file pointer
  139.  *    Returns:
  140.  *        SUCCESS         Files opened
  141.  *        FAIL            Error has occurred
  142.  */
  143. int OpenFiles(char **argv,TABLEHANDLE *tblHandlePtr, FILE **fpAscii)
  144. {
  145.  
  146.     if (OpenAsciiFile(argv[1],fpAscii) == FAIL)
  147.         return(FAIL);
  148.  
  149.     /* Open the Paradox file */
  150.     if (Error(PXTblOpen(argv[2],tblHandlePtr,0,0)))
  151.         return(FAIL);
  152.     return(SUCCESS);
  153. }
  154.         
  155. /*    Function:
  156.  *        OpenAsciiFile
  157.  *    Arguments:
  158.  *        fileName            Pointer to ASCII input file
  159.  *        fpAscii             Pointer to a file pointer
  160.  *    Returns:
  161.  *        SUCCESS             File was opened
  162.  *        FAIL                Could not open file
  163.  */
  164. int OpenAsciiFile(char *fileName, FILE **fpAscii)
  165. {
  166.     if ((*fpAscii = fopen(fileName, "r")) == NULL)
  167.     {
  168.         perror(fileName);
  169.         return(FAIL);
  170.     } 
  171.     else
  172.         return(SUCCESS);
  173. }
  174.  
  175. /*    Function:
  176.  *        CreateParadoxFile
  177.  *    Arguments:
  178.  *        fileName            Pointer to Paradox file names
  179.  *    Returns:
  180.  *        FAIL                Error has occurred
  181.  *        SUCCESS             File create successful
  182.  */
  183. int CreateParadoxFile(char *fileName)
  184. {
  185.     int exist;
  186.     /* Do not create if it already exists */
  187.     if (Error(PXTblExist(fileName, &exist))) 
  188.         return(FAIL);
  189.     if (exist) 
  190.     {
  191.         printf("IMPORT: Table %s already exists\n", fileName);
  192.         return(FAIL);
  193.     }
  194.  
  195.     /* Now attempt to create the table */
  196.     if (Error(PXTblCreate(fileName, NBRFIELDS, FieldNames, FieldTypes))) 
  197.         return(FAIL);
  198.  
  199.     /* Add first two fields as primary key */
  200.     if (Error(PXKeyAdd(fileName, NBRKEYS, keys, PRIMARY))) 
  201.         return(FAIL);
  202.     return(SUCCESS);
  203. }
  204.  
  205. /*    Function:
  206.  *        translate
  207.  *    Arguments:
  208.  *        tblHandle       Handle to a Paradox table
  209.  *        fpAscii         File pointer to ASCII input file
  210.  *    Returns:
  211.  *        SUCCESS         Translation successful
  212.  *        FAIL            Error in translation
  213.  */
  214. int translate(TABLEHANDLE tblHandle, FILE *fpAscii)
  215. {
  216.     AsciiRecord asciiBuf;
  217.     RECORDHANDLE recHandle;
  218.  
  219.     /* Setup a record handle */
  220.     if (Error(PXRecBufOpen(tblHandle, &recHandle))) 
  221.         return(FAIL);
  222.  
  223.     /* Read records until end of ASCII file */
  224.     while (! feof(fpAscii)) 
  225.     {
  226.         /* Check for end of file while reading buffer */
  227.         if (fscanf(fpAscii, "%s %s %d %s", asciiBuf.PartNbr, asciiBuf.CustNbr,
  228.         &asciiBuf.quantity, asciiBuf.date) != NBRFIELDS) 
  229.             break;
  230.         if (TranslateBuffer(&asciiBuf, recHandle) == FAIL)
  231.             return(FAIL);
  232.         /* And write it to the Paradox table */
  233.         if (Error(PXRecAppend(tblHandle, recHandle))) 
  234.             return(FAIL);
  235.     }
  236.  
  237.     /* File translated, close the record buffer */
  238.     if (Error(PXRecBufClose(recHandle))) 
  239.         return(FAIL);
  240.     return(SUCCESS);
  241. }
  242.  
  243. /*    Function:
  244.  *      TranslateBuffer
  245.  *    Arguments:
  246.  *      buf        Pointer to an AsciiRecord buffer
  247.  *      recHandle  Paradox Record Handle
  248.  *    Returns:
  249.  *        SUCCESS                 Translation successful
  250.  *        FAIL                    Translation failed
  251.  */
  252. int TranslateBuffer(AsciiRecord * buf, RECORDHANDLE recHandle)
  253. {
  254.     long PXDate;
  255.     int month, day, year;
  256.  
  257.     /* First the Customer Number */
  258.     if (Error(PXPutAlpha(recHandle, FIELDCUSTNBR, buf->CustNbr)))
  259.         return(FAIL);
  260.  
  261.     /* Next the Part Number */
  262.     if (Error(PXPutAlpha(recHandle, FIELDPARTNBR, buf->PartNbr))) 
  263.         return(FAIL);
  264.  
  265.     /* Quantity */
  266.     if (Error(PXPutDoub(recHandle, FIELDQUANTITY, (double) buf->quantity))) 
  267.         return(FAIL);
  268.  
  269.     /* To translate the date, first get month, day, and year from buffer
  270.        then use PXEncode to translate into a Paradox date format. */
  271.     sscanf(buf->date, "%2d/%2d/%4d", &month, &day, &year);
  272.  
  273.     if (Error(PXDateEncode(month, day, year, &PXDate)))
  274.         return(FAIL);
  275.  
  276.     /* Now put the date into the record buffer */
  277.     if (Error(PXPut